#include "User.h"
u16 count_id, init_bb, time_delay; // 消息ID
u8 m_wait_gps = 0;				   // 请求数据后等网络恢复数据
u8 m1, sys_tx_gps[100], sys_rx_gps[100], len_sys_gps;
u8 sys_rx_ic[50], len_sys_ic;
u8 sys_rx_print[50], len_sys_print;
u8 delay_opt;
u8 LW_Enable;
u8 motor_x[6], motor_y[6], JF_SUB[6];
u32 JF_user;
u8 sys_tx_qdb[30], sys_rx_qdb[30], len_sys_qdb, arry_m[50], sys_rx_ic_m[30];
u16 time_cyc = 1500; // 未接收到请求循环再次发送数据间隔 10ms
__int64 ID;
//------------------------------------------全局变量 GLOBAL_VAR--------------------------------
u8 Tx_281[8], Tx_282[8], Tx_283[8], Tx_284[8];
u8 Rx_181[8], Rx_182[8], Rx_183[8], Rx_184[8];
/** 输入 **/
u16 YouGanQiang_YaLi_mA; // 有杆腔压力传感器 mA 值
u16 WuGanQiang_YaLi_mA;	 // 无杆腔压力传感器 mA 值
u16 DangWei_QianHou_mV;	 // 前后挡拨轮
u8 DangWei_N;			 // N 档按钮
u8 DangWei_M;			 // M 档按钮
u8 Button_6;			 //
u16 ShouBing_Y_mV;		 // 手柄 Y 轴 mV 值
u16 ShouBing_X_mV;		 // 手柄 Y 轴 mV 值
u16 ShenSuo_BL_mV;		 // 伸缩拨轮 mV 值
u16 FuJu_BL_mV;			 // 辅具拨轮 mV 值
/** 输出 **/			 //
u16 FuJu_Valve_a;		 // 辅具阀a
u16 FuJu_Valve_b;		 // 辅具阀b
u16 Boom_UP;			 // 大臂升
u16 Boom_DOWN;			 // 大臂降
u16 FanDou_UP;			 // 翻斗
u16 FanDou_DOWN;		 // 收斗
u16 Boom_Shen;			 // 大臂伸出
u16 Boom_Suo;			 // 大臂缩回
static u8 n;			 // CAN发送循环计数
//------------------------------------------END_VAR--------------------------------
//===============================上升沿===========================================
u8 R_Trig(u8 in, u8 *p)
{
	if (in == 1 && (*p) == 0)
	{
		*p = in;
		return 1;
	}
	else
	{
		*p = in;
		return 0;
	}
}
//================================END_上升沿==========================================
//------------------------------------------FLASH(EEPROM)-------------------------------------
#define FLASH_SAVE_ADDR 0X08040000 // 设置FLASH 保存地址(必须为偶数，且所在扇区,要大于本代码所占用到的扇区.
								   // 否则,写操作的时候,可能会导致擦除整个扇区,从而引起部分程序丢失.引起死机.
u8 flash_byte[500];
u8 eeprom_En[1005], eeprom_En_TD[3];
u16 Nub_eeprom, total_eeprom, Nub_Print, mark_read, count_id2 = 111, CYC_G;
//
u8 Key_Ctrl_L, Key_Ctrl_R, Key_Ctrl_Enable, Pot_Ctrl, Mode_Ctrl;
//
u8 p_u8[15], CAN_ERR;
int p_int[15];
//
// u16 Joy_Fliter(u16 IN,u16 Par_min,u16 Par_max);

// #########################1.系统初始化##############################
int KOP_Init(void)
{
	// 3.CAN0口配置
	CAN1_Mode_Init(250);
	// 4.CAN1口配置
	CAN2_Mode_Init(250);
	return (200);
}
void CAN1_RX_Init(void)
{
	can_receive_setting1(0x201, CAN_ID_STD, 1, CAN_Filter_FIFO0);
}
void CAN2_RX_Init(void)
{
	can_receive_setting2(0x191, CAN_ID_STD, 14, CAN_Filter_FIFO0);
	can_receive_setting2(0x192, CAN_ID_STD, 15, CAN_Filter_FIFO0);
	can_receive_setting2(0x183, CAN_ID_STD, 16, CAN_Filter_FIFO0);
	can_receive_setting2(0x184, CAN_ID_STD, 17, CAN_Filter_FIFO0);
}
void IO_Congig(void)
{
	// 端口配置
	// 配置 < 0:0-5v 1:0-20mA 2:DI+ 3:DI- >
	ECU.PIN_3.Type = 1; // 有杆腔压力传感器
	ECU.PIN_4.Type = 1; // 无杆腔压力传感器
	ECU.PIN_5.Type = 0; // 手柄前进后退档拨轮
	ECU.PIN_6.Type = 2; // 手柄 N 按钮
	ECU.PIN_7.Type = 2; // 手柄 M 按钮
	/*
	 ** ECU.PIN_8.Type = 0; // 手柄 Y 轴
	 ** ECU.PIN_9.Type = 0; // 手柄 X 轴
	 ** ECU.PIN_16.Type = 0; // 手柄伸缩拨轮
	 ** ECU.PIN_17.Type = 0; // 手柄辅具拨轮
	 */
	// PWM_DO_DI 	< 0:DO 1:PWM 2:DI >
	ECU.PIN_2.Type = 1;	 // 辅具阀线圈a
	ECU.PIN_10.Type = 1; // 大臂阀举升
	ECU.PIN_11.Type = 1; // 大臂阀放下
	ECU.PIN_12.Type = 1; // 翻斗阀翻斗
	ECU.PIN_13.Type = 1; // 翻斗阀收斗
	ECU.PIN_25.Type = 1; // 大臂阀伸出
	ECU.PIN_26.Type = 1; // 大臂阀缩回
	ECU.PIN_29.Type = 1; // 辅具阀线圈b
	ECU.PIN_38.Type = 1; //
	ECU.PIN_39.Type = 0; //
	// PI/DI		< 0:DI 1:PI >
	ECU.PIN_14.Type = 1; //
	ECU.PIN_15.Type = 0; //
	ECU.PIN_18.Type = 0; //
	ECU.PIN_19.Type = 0; //
	/*
	//DI/AI     <PIN_8,PIN_9,PIN_16,PIN_17>
	无需配置，采集AI程序自动转为DI
		ECU.PIN_16				//四轮转向模式开关
		ECU.PIN_17				//蟹行转向模式开关
	*/
	// FB/DO-/DI< u8 Type;//0:DO 1:DI 2:FB >
	ECU.PIN_23.Type = 1; //
	ECU.PIN_24.Type = 1; //
	ECU.PIN_30.Type = 1;
	ECU.PIN_31.Type = 1;
	ECU.PWM_25_26_Set_PI.Par_PI = 200;
	ECU.PWM_12_13_10_11_Set_PI.Par_PI = 200;
	ECU.PWM_2_29_38_39_Set_PI.Par_PI = 200;
}
// ##########################2.主循环##################################
void KOP_Cycle(void)
{
	//===================输入采集========================
	YouGanQiang_YaLi_mA = ECU.PIN_3.AI;
	WuGanQiang_YaLi_mA = ECU.PIN_4.AI;
	DangWei_QianHou_mV = ECU.PIN_5.AI;
	DangWei_N = ECU.PIN_6.DI;
	DangWei_M = ECU.PIN_7.DI;
	ShouBing_Y_mV = ECU.PIN_8.AI;
	ShouBing_X_mV = ECU.PIN_9.AI;
	ShenSuo_BL_mV = ECU.PIN_16.AI;
	FuJu_BL_mV = ECU.PIN_17.AI;
	Button_6 = ECU.PIN_23.DI;

	Tx_281[0] = (YouGanQiang_YaLi_mA >> 0) & 0xFF;
	Tx_281[1] = (YouGanQiang_YaLi_mA >> 8) & 0xFF;
	Tx_281[2] = (WuGanQiang_YaLi_mA >> 0) & 0xFF;
	Tx_281[3] = (WuGanQiang_YaLi_mA >> 8) & 0xFF;
	Tx_281[4] = (ShouBing_X_mV >> 0) & 0xFF;
	Tx_281[5] = (ShouBing_X_mV >> 8) & 0xFF;
	Tx_281[6] = (ShouBing_Y_mV >> 0) & 0xFF;
	Tx_281[7] = (ShouBing_Y_mV >> 8) & 0xFF;

	Tx_282[0] = (DangWei_QianHou_mV >> 0) & 0xFF;
	Tx_282[1] = (DangWei_QianHou_mV >> 8) & 0xFF;
	Tx_282[2] = (ShenSuo_BL_mV >> 0) & 0xFF;
	Tx_282[3] = (ShenSuo_BL_mV >> 8) & 0xFF;
	Tx_282[4] = (FuJu_BL_mV >> 0) & 0xFF;
	Tx_282[5] = (FuJu_BL_mV >> 8) & 0xFF;
	Tx_282[6] = DangWei_N | DangWei_M << 1 | Button_6 << 2;
	Tx_282[7] = 0;

	Tx_283[0] = (ECU.PIN_10.PWM >> 0) & 0xFF;
	Tx_283[1] = (ECU.PIN_10.PWM >> 8) & 0xFF;
	Tx_283[2] = (ECU.PIN_11.PWM >> 0) & 0xFF;
	Tx_283[3] = (ECU.PIN_11.PWM >> 8) & 0xFF;
	Tx_283[4] = (ECU.PIN_12.PWM >> 0) & 0xFF;
	Tx_283[5] = (ECU.PIN_12.PWM >> 8) & 0xFF;
	Tx_283[6] = (ECU.PIN_13.PWM >> 0) & 0xFF;
	Tx_283[7] = (ECU.PIN_13.PWM >> 8) & 0xFF;

	n++;
	if (n % 10 == 1)
	{
		CAN_Send_Msg_FC(1, 0x01, CAN_Id_Standard, 8, Tx_283);
		CAN_Send_Msg_FC(2, 0x291, CAN_Id_Standard, 8, Tx_281);
		CAN_Send_Msg_FC(2, 0x292, CAN_Id_Standard, 8, Tx_282);
	}
	if (n % 10 == 2)
	{
		CAN_Send_Msg_FC(1, 0x02, CAN_Id_Standard, 8, Tx_281);
		CAN_Send_Msg_FC(1, 0x03, CAN_Id_Standard, 8, Tx_282);
		n = 0;
	}

	FuJu_Valve_a = (Rx_181[1] << 8) | Rx_181[0];
	FuJu_Valve_b = (Rx_181[3] << 8) | Rx_181[2];
	Boom_Shen = (Rx_181[5] << 8) | Rx_181[4];
	Boom_Suo = (Rx_181[7] << 8) | Rx_181[6];
	Boom_UP = (Rx_182[1] << 8) | Rx_182[0];
	Boom_DOWN = (Rx_182[3] << 8) | Rx_182[2];
	FanDou_UP = (Rx_182[5] << 8) | Rx_182[4];
	FanDou_DOWN = (Rx_182[7] << 8) | Rx_182[6];
	//===================输出刷新========================
	ECU.PIN_2.PWM = FuJu_Valve_a;
	ECU.PIN_10.PWM = Boom_UP;
	ECU.PIN_11.PWM = Boom_DOWN;
	ECU.PIN_12.PWM = FanDou_UP;
	ECU.PIN_13.PWM = FanDou_DOWN;
	ECU.PIN_25.PWM = Boom_Shen;
	ECU.PIN_26.PWM = Boom_Suo;
	ECU.PIN_29.PWM = FuJu_Valve_b;
	//=======================CAN========================
}
// #########################4.CAN1接收单元#############################
void KOP_CAN1_Event(void)
{
	CanRxMsg RxMessage;
	CAN_Receive(CAN1, 0, &RxMessage);
}
// #########################5.CAN2接收单元#############################
void KOP_CAN2_Event(void)
{
	static u32 ii;
	CanRxMsg RxMessage;
	CAN_Receive(CAN2, 0, &RxMessage);
	if (RxMessage.StdId == 0x191)
	{
		for (ii = 0; ii < 8; ii++)
		{
			Rx_181[ii] = RxMessage.Data[ii];
		}
	}
	if (RxMessage.StdId == 0x192)
	{
		for (ii = 0; ii < 8; ii++)
		{
			Rx_182[ii] = RxMessage.Data[ii];
		}
	}
}
